import Mock from 'mockjs'

//模拟用户
var users = [
    { id: 1, name: '李莉', phone: 13611129070, pass: '1234', roles: ['home', 'contact', 'order', 'a'] },
    { id: 2, name: '李丽', phone: 13611129071, pass: '1234', roles: ['home', 'contact', 'order', 'a', 'b'] },
    { id: 3, name: '丽丽', phone: 13611129072, pass: '1234', roles: ['home', 'contact', 'order', 'a', 'b', 'c'] }
]

//模拟接口
Mock.mock('/api/user/login', 'post', function (req) {
    //获取参数
    var { phone, pass } = JSON.parse(req.body);

    //判断用户是否合法 ,  some方法: 判断数组中是否有元素满足指定条件, 如果有元素能满足指定条件 返回true, 否则返回false
    // var res = users.some((item)=>{
    //     return item.phone == phone && item.pass == pass;
    // })
    var res = users.find((item) => {
        return item.phone == phone && item.pass == pass;
    })
    if (res) {
        delete res.pass;
        return { code: 200, userinfo: res, msg: '登陆成功' }
    } else {
        return { code: 400, msg: '登陆失败' }
    }

})